<?php
namespace Modules\Base\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Services\WechatService;
use Houdunwang\Module\Traits\Db;
use Illuminate\Http\Request;
use Modules\Base\Entities\BaseReply;
use Modules\Base\Http\Requests\BaseReplyRequest;
use Modules\Wx\Entities\WxKeywords;
use Modules\Wx\Entities\WxRule;

class BaseReplyController extends Controller
{
    //显示列表
    public function index()
    {
        $data = BaseReply::paginate(10);
        return view('base::base_reply.index', compact('data'));
    }

    //创建视图
    public function create(BaseReply $base_reply,WechatService $wechatService)
    {
        $view = $wechatService->ruleView();
        return view('base::base_reply.create',compact('base_reply','view'));
    }

    //保存数据
    public function store(BaseReplyRequest $request,BaseReply $base_reply,WechatService $wechatService)
    {
        //调用服务类中的方法处理规则和关键词
        $rule = $wechatService->saveRule();
        //添加回复内容
        BaseReply::create([
            'content'=>$request->input('content'),
            'rule_id'=>$rule['id'],
        ]);

        return redirect('/base/base_reply')->with('success', '保存成功');
    }

    //显示记录
    public function show(BaseReply $field)
    {
        return view('base::base_reply.show', compact('field'));
    }

    //编辑视图
    public function edit(BaseReply $base_reply,WechatService $wechatService)
    {
        $view = $wechatService->ruleView($base_reply['rule_id']);
        //dd($base_reply)->toArray();
        return view('base::base_reply.edit', compact('base_reply','view'));
    }

    //更新数据
    public function update(BaseReplyRequest $request, BaseReply $base_reply,WechatService $wechatService)
    {
        //调用服务类的保存方法
        $rule = $wechatService->saveRule($base_reply['rule_id']);
        //将回复内容数据存入BaseReply表
        $base_reply->content = $request->input('content');
        $base_reply->rule_id = $rule['id'];
        $base_reply->save();
        return redirect('/base/base_reply')->with('success','更新成功');
    }

    //删除模型
    public function destroy(BaseReply $base_reply)
    {
        //删除规则表
        WxRule::destroy($base_reply['rule_id']);
        $base_reply->delete();
        return redirect('base/base_reply')->with('success','删除成功');
    }
}
